/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cinescope2014.utilities;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author pascal
 */
public class UtilitairesBD2IHM {

    /**
     *
     * @param acn
     * @param asSelect
     * @param cle
     * @param valeur
     * @return
     */
    public static Map sql2Map(Connection acn, String asSelect, String cle, String valeur) {
        Map<String, String> map = new HashMap();

        try {

            PreparedStatement lpst = acn.prepareStatement(asSelect);
            ResultSet lrs = lpst.executeQuery();
            while (lrs.next()) {
                map.put(lrs.getString(cle), lrs.getString(valeur));
            }
            lrs.close();
            lpst.close();
        } catch (SQLException e) {
        }

        return map;
    } /// sql2Map
    
    /**
     * 
     * @param acn
     * @param asSelect
     * @return 
     */
    public static String[] sql2Array(Connection acn, String asSelect) {
        List<String> liste = new ArrayList();
        String[] t = null;

        try {

            PreparedStatement lpst = acn.prepareStatement(asSelect);
            ResultSet lrs = lpst.executeQuery();
            while (lrs.next()) {
                liste.add(lrs.getString(1));
            }
            t = liste.toArray(new String[liste.size()]);
            lrs.close();
            lpst.close();
        } catch (SQLException e) {
        }

        return t;
    } /// sql2Map    
} /// class UtilitairesBD2IHM
